import axios,{ type InternalAxiosRequestConfig,type AxiosError,type AxiosRequestConfig } from "axios";

const instance=axios.create({
  baseURL:'http://localhost:8989',
  timeout:2000
})

instance.interceptors.request.use(
  (config:InternalAxiosRequestConfig)=>{
    config.headers.set('Content-Type','application/json')
    if(config.method == 'post'){
      config.data=JSON.stringify(config.data)
    }
    // return {
    //   ...config,
    //   headers:{
    //     'x-access-token':''
    //   }
    // }
    return config
  },
  (error:AxiosError)=>{
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  (response)=>{
    return response.status == 200 ? Promise.resolve(response) : Promise.reject(response)
  },
  (error:AxiosError)=>{
    if(error.response?.status == 401){
      window.location.href='/LoginView'
    }else if(error.response?.status == 500){
      console.error('服务器错误');
    }
    return Promise.reject(error)
  }
)
